import {
  BlinnPhongMaterial,
  Engine,
  Entity,
  MeshRenderer,
  ModelMesh,
  PrimitiveMesh,
} from "oasis-engine";
import { createAxesEntity } from "oasis-toolbox";
import { GameConfig } from "../../GameConfig";
import { Table } from "../../type";
import { TableScript } from "./TableScript";

export class TableManager {
  private cuboidMesh: ModelMesh;
  private cuboidMaterial: BlinnPhongMaterial;
  private tableHeight: number = GameConfig.tableHeight;
  private tableSize: number = 16;

  constructor(private engine: Engine, private sceneEntity: Entity) {
    this.cuboidMesh = PrimitiveMesh.createCuboid(
      engine,
      this.tableSize,
      this.tableHeight,
      this.tableSize
    );
    this.cuboidMaterial = new BlinnPhongMaterial(engine);
    this.cuboidMaterial.baseColor.setValue(1, 0, 0, 1);
  }

  createCuboid(
    x: number,
    y: number,
    z: number,
    needEnterAnimation = true
  ): Table {
    const cuboid = this.sceneEntity.createChild("cuboid");
    const tableScript = cuboid.addComponent(TableScript);
    tableScript.needAnimation = needEnterAnimation;
    const renderEntity = cuboid.createChild("render");
    renderEntity.transform.setPosition(0, 5, 0);
    cuboid.transform.setPosition(x, y, z);
    const renderer = renderEntity.addComponent(MeshRenderer);
    renderer.mesh = this.cuboidMesh;
    renderer.setMaterial(this.cuboidMaterial);
    return { entity: cuboid, size: this.tableSize };
  }

  clearAll() {
    this.sceneEntity.clearChildren();
  }
}
